import unittest
import process_mail as mail
import file_operations as file
from htmlParser import *
from document import *

class TestProcessMail(unittest.TestCase):
    def setUp(self):
        FILE_ADDRESS = "data/mail/1.mail"
        self.mail_content = file.read(FILE_ADDRESS)
        self.new_doc = document([], "first mail", "mail")
        self.header, self.body = mail.getHeaderAndBody(self.mail_content)
        self.header_list = mail.createHeaderList(self.header)
        
    def testParseHTML(self):
        x = mail.parseMail(self.mail_content, self.new_doc)
        self.assertEqual(x, ['forecast'])
    
    def testGetHeaderAndBody(self):
        self.assertEqual(self.header, "Message-ID: <18782981.1075855378110.JavaMail.evans@thyme>\nDate: Mon, 14 May 2001 16:39:00 -0700 (PDT)\nFrom: phillip.allen@enron.com\nTo: tim.belden@enron.com\nSubject: \nMime-Version: 1.0\nContent-Type: text/plain; charset=us-ascii\nContent-Transfer-Encoding: 7bit\nX-From: Phillip K Allen\nX-To: Tim Belden <Tim Belden/Enron@EnronXGate>\nX-cc: \nX-bcc: \nX-Folder: \\Phillip_Allen_Jan2002_1\\Allen, Phillip K.\\'Sent Mail\nX-Origin: Allen-P\nX-FileName: pallen (Non-Privileged).pst")
        self.assertEqual(self.body, "Here is our forecast\n\n ")
    
    def testParseMailHeader(self):
        mail.parseMailHeader(self.header, self.new_doc)
        self.assertEqual(self.new_doc.title, [])
        self.assertEqual(self.new_doc.authors, ['phillip', 'k', 'allen', 'tim', 'belden', 'tim', 'beldenenronenronxgate', 'phillip.allen@enron.com', 'tim.belden@enron.com'])
        
    def testGetAuthorNames(self):
        mail.getAuthorNames(self.new_doc, self.header_list)         
        self.assertEqual(self.new_doc.authors, ['phillip', 'k', 'allen', 'tim', 'belden', 'tim', 'beldenenronenronxgate'])
        
    def testCreatHeaderList(self):
        self.assertEqual(self.header_list, ['Message-ID:', '<18782981.1075855378110.JavaMail.evans@thyme>', 'Date:', 'Mon,', '14', 'May', '2001', '16:39:00', '-0700', '(PDT)', 'From:', 'phillip.allen@enron.com', 'To:', 'tim.belden@enron.com', 'Subject:', 'Mime-Version:', '1.0', 'Content-Type:', 'text/plain;', 'charset=us-ascii', 'Content-Transfer-Encoding:', '7bit', 'X-From:', 'Phillip', 'K', 'Allen', 'X-To:', 'Tim', 'Belden', '<Tim', 'Belden/Enron@EnronXGate>', 'X-cc:', 'X-bcc:', 'X-Folder:', '\\Phillip_Allen_Jan2002_1\\Allen,', 'Phillip', "K.\\'Sent", 'Mail', 'X-Origin:', 'Allen-P', 'X-FileName:', 'pallen', '(Non-Privileged).pst'])
    
    def testGetTitle(self):
        mail.getTitle(self.new_doc, self.header_list)
        self.assertEqual(self.new_doc.title, [])
    
    def testGetAuthorAddresses(self):
        mail.getAuthorAddresses(self.new_doc, self.header_list)
        self.assertEqual(self.new_doc.authors, ['phillip.allen@enron.com', 'tim.belden@enron.com'])
    
    def testFindNextTag(self):
        x = mail.findNextTag(mail.SUBJECTTAG, self.header_list)
        self.assertEqual(x, mail.MIMETAG)
    
    def testProcessAuthors(self):
        mail.processAuthors(self.new_doc, self.header_list)
        self.assertEqual(self.new_doc.authors, ['phillip', 'k', 'allen', 'tim', 'belden', 'tim', 'beldenenronenronxgate', 'phillip.allen@enron.com', 'tim.belden@enron.com'])
    
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestProcessMail)
    unittest.TextTestRunner(verbosity=2).run(suite)